//
//  Address_bookViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/13.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Address_bookViewController.h"

#import "Address_bookTableViewCell.h"

@interface Address_bookViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
//头视图
@property (nonatomic, strong) UIView * view_Head;

//数据
@property (nonatomic, strong) NSMutableArray * arr_record;

@end

@implementation Address_bookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"工作提醒设置";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"隐私";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    //头视图
    [self p_headView];
    self.tableView.tableHeaderView = self.view_Head;
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[Address_bookTableViewCell class] forCellReuseIdentifier:@"Address_book"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Address_bookTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Address_book" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    switch (indexPath.row)
    {
        case 0:
        {
            cell.name.text = @"@网建项目部";
            cell.image_Select.hidden = NO;
        }
            break;
        case 1:
        {
            cell.name.text = @"@技术中心";
            cell.image_Select.hidden = NO;
        }
            break;
        default:
            break;
    }
    
    for (NSString * str_record in self.arr_record)
    {
        if([str_record integerValue] == indexPath.row)
        {
            cell.image_Select.hidden = YES;
        }
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL is_ok = 0;
    
    for (NSString * str_record in self.arr_record)
    {
        if([str_record integerValue] == indexPath.row)
        {
            is_ok = 1;
        }
    }
    
    if(is_ok == 0)
    {
        [self.arr_record addObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
    }
    else
    {
        [self.arr_record removeObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

#pragma mark - 头视图
- (void )p_headView
{
    self.view_Head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    self.view_Head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH - 30, 20)];
    detail.text = @"取消勾选将不再收到@这个部门的工作提醒";
    detail.textColor = [UIColor grayColor];
    detail.font = [UIFont systemFontOfSize:14.0f];
    detail.numberOfLines = 1;
    [self.view_Head addSubview:detail];
}

#pragma mark - 懒加载
- (NSMutableArray *)arr_record
{
    if(_arr_record == nil)
    {
        self.arr_record = [NSMutableArray array];
    }
    
    return _arr_record;
}


@end
